const { trainModel } = require("../models");
//查找所有教练
async function getAlltrains() {
    let result = await trainModel.find();
    return result;
}

//所有教练的个数
async function getAlltrainsCount() {
    let result = await trainModel.find().count();
    return result;
}

//分页
async function list(filter, page = 1, limit = 10) {
    let result = await trainModel
        .find(filter)
        .skip((page - 1) * limit)
        .limit(limit);
    return result;
}
async function count(filter) {
    let data = await trainModel.find(filter).count();
    return data;
}

//删除
async function deleteById(id) {
    let data = await trainModel.deleteOne({ "_id": id })
   
    return data;
}

//添加
async function add(train) {
    let result = await trainModel.create(train);
    return result;
}

//查询
async function findOne(_id) {
    let data = await trainModel.findById(_id);
    return data;
}

//修改
async function updata(train) {
    let data = await trainModel.updateOne({ _id: train._id }, train);
    return data;
}


module.exports.getAlltrains = getAlltrains;
module.exports.getAlltrainsCount = getAlltrainsCount;
module.exports.add = add;
module.exports.update = updata;
module.exports.findOne = findOne;
module.exports.list = list;
module.exports.count = count;
module.exports.deltetById = deltetById;